package online.chazi.bootplus.commons.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 说明：作者很懒，什么都没留下
 * Created by 叉子同学 on 2020-03-09 18:16
 */
public class GZipUtils {

    public static byte[] compress(byte[] input) throws Exception {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            GZIPOutputStream out = new GZIPOutputStream(os);
            out.write(input);
            out.flush();
            return os.toByteArray();
        }
    }

    public static byte[] uncompress(byte[] input) throws Exception {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            ByteArrayInputStream in = new ByteArrayInputStream(input);
            GZIPInputStream inputStream = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                os.write(buffer, 0, len);
            }
            os.flush();
            in.close();
            return os.toByteArray();
        }
    }

}
